from postfunctions import *
from collector import *
from BClassifier import *
from dbtrainer import *

import os
import datetime

if __name__=="__main__":
	
	import sys
	os.chdir("..")
	path=os.getcwd()
	sys.path += [path]
	os.environ['DJANGO_SETTINGS_MODULE'] = 'categories.settings'
	os.chdir("categories")
	
	
	updatetime = datetime.datetime.now()
	
	updateinfofile = open("update.info","w")
	updateinfo = str(updatetime.day)+"/"+str(updatetime.month)+"/"+str(updatetime.year)+" "+str(updatetime.hour)+":"+str(updatetime.minute)
	updateinfofile.write(updateinfo)
	updateinfofile.close()
	
	timeknowledge=cfgread("venga.cfg")
	
	import time
	
	#while(True):
	#flag = True
	#if timeknowledge[0]=="day":
		#sleeptime=timeknowledge[1]*24*60*60
	#if timeknowledge[0]=="minutes":
		#sleeptime=timeknowledge[1]*60*60
		
	c=Collector()
	lst = c.openxmlsrc("xml-resources")
	
	for files in lst:
		src = c.parser(files)
		c.createfiles(src)
		c.createfiles(src,1)
		
	li = check()
	c=BClassifier()
	c.summaryfilescreate(c,li)
			
	for x,y,z in os.walk("files"):
		lst=z
		i=0
		for f in lst:
			if f.find("wordlistsummary") != -1 and li[f[:-15]] == True: pass
			else: continue
			print f
			 
			t=DBtrainer()
			import time
			starttime=time.time() 
			category = t.calculateprobability("files/"+f)
			endtime = time.time()
			print "category time=", endtime-starttime
			girdiname = f.split("wordlistsummary")[0]+"girdi"
			girdi = open("files/"+girdiname,"r")
			fl=girdi.readlines()
			
			yzr=fl[0].strip()
			#print yzr
			bslk=fl[1].strip()
			#print bslk
			trh=datefixer(fl[2].strip())
			#print trh
			lnk=girdiname.replace("--","/")
			lnk=lnk[:-5]
			#print lnk
			crk=fl[3:][0]
			
			if len(category) == 0:
				something=__import__("categories.categoriesdb.models")
				clsname="diger"+"girdi"
				c=something.categoriesdb.models.__getattribute__(clsname)
				y=c(yazar=str(yzr), baslik=str(bslk), link=str(lnk), tarih=str(trh), icerik=str(crk))
				y.save()
			
			else:
				for h in category:
					something=__import__("categories.categoriesdb.models")
					clsname=h+"girdi"
					c=something.categoriesdb.models.__getattribute__(clsname)
					y=c(yazar=str(yzr), baslik=str(bslk), link=str(lnk), tarih=str(trh), icerik=str(crk))
					y.save()
			
			del fl # finished with fl list
			
			fname = open("files/"+f,"r")
			f=fname.readlines()
			
			for element in f:
							
				li = element.strip().split(" ")
				if len(li) == 1 or len(li) == 0: continue ## that is something like one keyword without a frequency
				for v in category:	
					clsname=v+"frekans"
						
					#print clsname	
					something=__import__("categories.categoriesdb.models")
					c=something.categoriesdb.models.__getattribute__(clsname)
					try:
						check=c.objects.get(kelime=li[0])
						value = int(check.frekans)+int(li[1])
						check.frekans=value
						check.save()
					except:
						y=c(kelime=li[0], frekans=int(li[1]))
						y.save()
			del f ## finished with f list			
			girdi.close()
			fname.close()
	lst.close()					
	print "bitti"
	#time.sleep(sleeptime)
	os.getcwd("files")
	os.peopen2("rm -rf *")
	os.chdir("..")
		